<template>
  <div class="file-container">
    <h2 class="setting-header">
      <strong>文件管理</strong>
      <el-button type="primary" v-if="isAdmin" @click="showBucketList = true" style="margin-left: 10px">文件桶管理</el-button>
      <el-button type="primary" v-if="isDev" @click="showFilesUpload = true" style="margin-left: 10px">上传附件test</el-button>
      <el-button type="primary" v-if="isDev" @click="showImageUpload = true" style="margin-left: 10px">上传图片test</el-button>
      <el-button type="" @click="handleRefresh" :loading="refreshLoading" style="margin-left: 10px">刷新</el-button>
    </h2>
    <!-- 文件桶列表 -->
    <div class="content-box" v-if="showBucketList && isAdmin">
      <el-table :data="bucketList" class="bucket-table" v-loading="bucketListLoading">
        <el-table-column label="名称">
          <template #default="{ row }">
            <div class="bucket-item">
              <i class="iconfont">&#xe64c;</i>
              <span class="bucket-name" @click="handleBucketClick(row)">{{ row.bucketName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createDate" />
      </el-table>
    </div>
    <!-- 文件夹和文件列表 -->
    <div class="content-box" v-else>
      <FileList v-model:showBucketList="showBucketList" :curBucket="curBucket" :prefix="prefix" :pageRefresh="pageRefresh" :isAdmin="isAdmin" ref="filesUploadRef" />
    </div>
    <FilesUpload v-model:showDialog="showFilesUpload" @getFileList="insertFiles"></FilesUpload>
    <ImageUpload v-model:showDialog="showImageUpload" :limit="20" @getImage="insertImage"></ImageUpload>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { bucketSelectAll, bucketRepair } from '@/api/file'
import FileList from '@/views/file/manage/components/File'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'

const store = useStore()
const isAdmin = ref(false)
const userInfo = store.getters.userInfo
// 判断是否是超管
if (userInfo.roleIds.includes(0)) isAdmin.value = true
// 默认展示桶列表
const showBucketList = ref(true)
//是否刷新文件列表页面
const pageRefresh = ref(false)

const isDev = ref(process.env.NODE_ENV === 'development')
// 修复文件桶 测试环境用
if (isDev.value) bucketRepair()

// 查看所有存储桶
const curBucket = ref(null)
const defaultBucketQueryData = {}
const bucketQueryData = ref(structuredClone(defaultBucketQueryData))
const bucketList = ref([])
const bucketListLoading = ref(false)
const hanldeBucketSelectAll = async () => {
  bucketListLoading.value = true
  let res = await bucketSelectAll(bucketQueryData.value)
  bucketList.value = res.data
  bucketListLoading.value = false
}
if (isAdmin.value) {
  hanldeBucketSelectAll()
}

// 上传附件
const showFilesUpload = ref(false)
const insertFiles = filesList => {
  console.log('filesList', filesList)
}
// 上传图片
const showImageUpload = ref(false)
const insertImage = filesList => {
  console.log('filesList', filesList)
}
// 点击桶 查询文件
const handleBucketClick = val => {
  fileDataInit()
  curBucket.value = val
  showBucketList.value = false
}

// 刷新
const filesUploadRef = ref()
const _fileQueryData = ref()
const prefix = ref([])
//初始化文件列表参数
const fileDataInit = () => {
  _fileQueryData.value = null
  prefix.value = []
  pageRefresh.value = false
}
//检测是否在文件列表页刷新页面
const handlePageRefresh = () => {
  _fileQueryData.value = JSON.parse(window.sessionStorage.getItem('fileQueryData.value'))
  if (_fileQueryData.value) {
    if (_fileQueryData.value.prefix) {
      prefix.value = _fileQueryData.value.prefix.split('/')
      prefix.value.pop() //移除最后的空项
    }
    pageRefresh.value = true //防止子组件重复调用handleGetBucketFile方法
    showBucketList.value = false
    setTimeout(() => {
      if (filesUploadRef.value) {
        filesUploadRef.value.fileQueryData = _fileQueryData.value
        filesUploadRef.value.handleGetBucketFile()
      }
    }, 0)
  }
}
handlePageRefresh()

// 刷新按钮
const refreshLoading = ref(false)
const handleRefresh = () => {
  refreshLoading.value = true
  if (showBucketList.value) {
    //刷新桶列表
    hanldeBucketSelectAll()
  } else {
    // 刷新文件列表
    _fileQueryData.value = JSON.parse(window.sessionStorage.getItem('fileQueryData.value'))
    filesUploadRef.value.fileQueryData = _fileQueryData.value
    filesUploadRef.value.handleGetBucketFile()
  }
  setTimeout(() => {
    ElMessage({
      showClose: true,
      message: `刷新成功`,
      type: 'success'
    })
    refreshLoading.value = false
  }, 300)
}
</script>
<style scoped lang="scss">
.file-container {
  width: 100%;
  min-width: 1024px;
  box-sizing: border-box;
  background-color: #fff;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  border-radius: 5px;
  padding: 20px 20px;
  h2 {
    font-weight: normal;
    strong {
      font-size: 18px;
      font-weight: bold;
      margin-right: 20px;
    }
  }
  .search-wrap {
    display: flex;
    align-items: center;
    justify-content: space-between;
    & > div {
      display: flex;
      align-items: center;
    }
    .right-input {
      .el-input {
        margin-left: 10px;
      }
    }
  }
  .content-box {
    min-height: calc(100vh - 230px);
    max-height: calc(100vh - 210px);
    .bucket-table {
      width: 100%;
      margin-top: 10px;
      .bucket-item {
        display: flex;
        align-items: center;
        i {
          margin-right: 5px;
          font-weight: bold;
          color: $mainColor;
        }
        .bucket-name {
          cursor: pointer;
          &:hover {
            font-weight: bold;
          }
        }
      }
    }
  }
}
</style>
